﻿#ifndef POLYGONFEATURE_H
#define POLYGONFEATURE_H

#include <QGraphicsObject>
#include <QJsonArray>
#include <QPolygonF>
#include <QString>

#include "feature.h"

class PolygonFeature : public Feature {
  Q_OBJECT
 public:
  PolygonFeature(QGraphicsItem *parent = NULL);
  PolygonFeature(const QString &name, QGraphicsItem *parent = NULL);
#if 0
  PolygonFeature(const QString &name, int id);
  PolygonFeature(const QString &name, const QPolygonF &poly);
#endif

  // copy the data from @polygon, used in subclasses
  void copy(PolygonFeature &polygon);
  // setters and getters
  QPolygonF &outline();
  void setOutline(const QVector<QPointF> &points);
  double area();
  void setArea(const double area);

  // add a point to tail
  void addPoint(const QPointF &p);

  // move the point at @id by @vector
  void movePoint(const int id, const QPointF &vector);

  // move the point at @id to @point
  void movePointTo(const int id, const QPointF &point);

  // insert a @point at @id
  void insertPoint(const int id, const QPointF &p);

  // remove the point at @id
  void removePoint(const int id);

  // remove all point
  void removeAllPoint();

  int PointNum() const;

  // set color
  void setColor(QColor color);

  // compute the center
  const QPointF &computeCenter();

  // compute the area
  double computeArea();

  // compute the OBB (oriented bounding box) main direction
  QPointF computeMainDir();

  // QGraphicsItem functions
  QRectF boundingRect() const;
  QPainterPath shape() const;
  void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
             QWidget *widget);

  // io
  virtual bool load(const QJsonObject &jsonObject);
  virtual bool save(QJsonObject &jsonObject) const;

  virtual void transformFeature(const QMatrix &matrix);

 protected:
  QPolygonF m_outline;
  QColor m_color;
  double m_area;
  double m_frontAngle;
};

#endif  // POLYGONFEATURE_H
